import { defineStore } from 'pinia'

import { reqLogin } from "@/api/user/index"
import { loginForm, loginResponseData } from "@/api/user/type.ts"
import { userState } from "../types/user.ts"
import { toRaw } from 'vue'
let userStore = defineStore('User', {
    state: (): userState => {
        return {
            token: localStorage.getItem('token'),
            userInfo: {}, //用户信息
        }
    },
    actions: {
        async loginForm(loginForm: loginForm) {
            let res: loginResponseData = await reqLogin(toRaw(loginForm))
            if (res.code == 200 && res.data.token) {
                this.token = res.data.token
                console.log("====>",this.token);

                localStorage.setItem('token', res.data.token)
                return 'ok'
            } else if (res.code == 201) {
                return Promise.reject(new Error(res.data.message))
            }
        },
    },
    getters: {

    }
})

export default userStore